package com.ghline.caloncabe;

import java.util.Random;

import android.graphics.Canvas;
import com.anhthao.nuotcabe.R;

public class GameThread extends Thread {
	
	public static final int STATE_PAUSE = 100;
	public static final int STATE_RUNNING = 101;
	public static final int STATE_LOSE = 102;
	public static final int STATE_WIN = 103;
	public static final int STATE_SHOW_MENU = 104;
	public static final int STATE_SHOW_LEVEL = 105;
	public static final int STATE_HELP = 106;
	
	public int currentState;

	static final long FPS = 10;
	private GameView view;
	private boolean running = false;

	public boolean isRunning() {
		return running;
	}

	public GameThread(GameView view) {
		this.view = view;
	}

	public void setRunning(boolean run) {
		running = run;
	}

	@Override
	public void run() {

		long ticksPS = 1000 / FPS;
		long startTime;
		long sleepTime;
		
		while (running) {
			Canvas c = null;
			startTime = System.currentTimeMillis();

			try {
				c = view.getHolder().lockCanvas();
				
				synchronized (view.getHolder()) {
					if (currentState == STATE_RUNNING) {					
						view.onDraw(c);
						view.update();
					} else 	
					if (currentState == STATE_SHOW_MENU) {
						view.updateMainMenu();
						view.drawMainMenu(c);
					}
					if (currentState == STATE_PAUSE) {
						view.updatePauseMenu();
						view.drawMenuPause(c);
					}		
					if (currentState == STATE_LOSE) {
						view.drawMenuOver(c);
					}
					if (currentState == STATE_WIN) {
						view.drawMenuComplete(c);
					}
					if (currentState == STATE_SHOW_LEVEL) {
						view.drawLevelMenu(c);
					}
					if (currentState == STATE_HELP) {
						view.drawHelp(c);
					}
				}
			} finally {
				if (c != null) {
					view.getHolder().unlockCanvasAndPost(c);
				}
			}
			sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
			try {
				if (sleepTime > 0)
					sleep(sleepTime);
				else
					sleep(10);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public void setState(int state) {
		this.currentState = state;
	}
	
	public void pause()
	{
		synchronized (view.getHolder())
		{
			setState(STATE_PAUSE);
		}
	}
	
	public void unpause()
	{
		synchronized (view.getHolder())
		{
			setState(STATE_RUNNING);
		}
	}
	
	public void startGame() {
		
	}
	
	public void restart() {
		
	}
	
	public void resetGame() {
		view.setScore(0);
		view.setProgress(0);
		view.setLife(GameView.MAX_LIFE);
		Random rnd = new Random();
        int x = rnd.nextInt(view.getWidth() - view.getSpriteBig().getWidth());
        int y = rnd.nextInt(view.getHeight() - view.getSpriteBig().getHeight());
        view.getSpriteSmall().setX(x);
        view.getSpriteMedium().setX(x);
        view.getSpriteBig().setX(x);
        view.getSpriteSmall().setY(y);
        view.getSpriteMedium().setY(y);
        view.getSpriteBig().setY(y);
		view.getFish().setBmp(view.getFishSmallBitmap());
		view.getFish().setX(view.getWidth() / 2 - view.getFish().getWidth() / 2);
		view.getFish().setY(view.getHeight() / 2 - view.getFish().getHeight() / 2);
		view.getFish().setWidth(view.getFishSmallBitmap().getWidth() / 2);
		view.getFish().setHeight(view.getFishSmallBitmap().getHeight() / 2);
		view.setCanMove(false);
		setRunning(true);
	}
	
	public void resetToNextLevel() {
		view.setScore(0);
		view.setProgress(0);
		view.setLife(view.getLife());
		view.getFish().setBmp(view.getFishSmallBitmap());
		view.getFish().setX(view.getWidth() / 2 - view.getFish().getWidth() / 2);
		view.getFish().setY(view.getHeight() / 2 - view.getFish().getHeight() / 2);
		view.getFish().setWidth(view.getFishSmallBitmap().getWidth() / 2);
		view.getFish().setHeight(view.getFishSmallBitmap().getHeight() / 2);
		view.setCanMove(false);
		setRunning(true);
	}
}
